package com.wx.controller;

import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.CheckDictCodeDto;
import com.wx.domain.vo.DicTreeVo;
import com.wx.entity.SysDict;
import com.wx.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: SysDictController<br>;
 * Description: 数据字典controller<br>;
 * Copyright: Copyright (c) 2017<br>;
 * Company: <br>;
 *
 * @author xinglinglove1029@163.com<br>
 * 2018/11/21 16:53
 */
@RestController
@RequestMapping(value = "/dict", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "数据字典", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysDictController{
    
    @Resource
    private SysDictService sysDictService;



    /**
     * <p>Title:     getDicTree. </p>
     * <p>Description 查询数据字典树</p>
     *
     * @return Dic tree
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /4/27 11:54
     */
    @PostMapping(value = "/getDicTree")
    @ApiOperation(httpMethod = "POST", value = "查询数据字典树")
    public Wrapper<List<DicTreeVo>> getDicTree() {
        List<DicTreeVo> dicTreeVos = sysDictService.getDicTree();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, dicTreeVos);
    }

    /**
     * <p>Title:     getDicById. </p>
     * <p>Description 根据数据字典编码查询数据在字典信息</p>
     *
     * @param         dicId String
     * @return      Wrapper
     * @Author        <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since     2018/11/21 16:57
     */
    @PostMapping(value = "/getDicById/{dicId}")
    @ApiOperation(httpMethod = "POST", value = "根据数据字典编码查询数据在字典信息")
    public Wrapper<SysDict> getDicById(@PathVariable String dicId) {
        SysDict dic = sysDictService.getDicById(dicId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, dic);
    }

    /**
     * <p>Title:      getChildrenById. </p>
     * <p>Description 根据父编码查询数据字典信息</p>
     *
     * @param       parentCode  String
     * @return        Wrapper
     * @Author        <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since     2019/9/26 16:28
     */
    @PostMapping(value = "/getChildrenByCode/{parentCode}")
    @ApiOperation(httpMethod = "POST", value = "根据父编码查询数据字典信息")
    public Wrapper<List<SysDict>> getChildrenById(@PathVariable String parentCode) {
        List<SysDict> sysDictList = sysDictService.getChildrenByCode(parentCode);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysDictList);
    }

    /**
     * <p>Title:      recursionChildrenByParentCode. </p>
     * <p>Description 根据父编码递归查询数据字典信息</p>
     *
     * @param       parentCode  String
     * @return        Wrapper
     * @Author        <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since     2019/9/26 16:28
     */
    @PostMapping(value = "/recursionChildrenByParentCode/{parentCode}")
    @ApiOperation(httpMethod = "POST", value = "根据父编码递归查询数据字典信息")
    public Wrapper<List<SysDict>> recursionChildrenByParentCode(@PathVariable String parentCode) {
        List<SysDict> sysDictList = sysDictService.recursionChildrenByParentCode(parentCode);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, sysDictList);
    }

    /**
     * <p>Title:      checkDictCode. </p>
     * <p>Description 校验数据字典编码唯一性</p>
     *
     * @param checkDictCodeDto the check user name dto
     * @return wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/24 17:08
     */
    @PostMapping(value = "/checkDictCode")
    @ApiOperation(httpMethod = "POST", value = "校验数据字典编码唯一性")
    public Wrapper<Boolean> checkDictCode(@ApiParam(name = "checkDictCode", value = "用户dto") @RequestBody  @Validated() CheckDictCodeDto checkDictCodeDto) {
        boolean flag = false;
        String dictId = checkDictCodeDto.getDictId();
        String dictCode = checkDictCodeDto.getDictCode();
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictCode", dictCode);
        if (StringUtils.isNotEmpty(dictId)) {
            criteria.andNotEqualTo("id", dictId);
        }
        int result = sysDictService.selectCountByExample(example);
        if(result > 0) {
            flag = true;
        }
        return WrapMapper.ok(flag);
    }
}
